﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;
using System.IO.IsolatedStorage;
using System.IO;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using GameStateManagement;
using Microsoft.Phone.Shell;
using System.Xml.Serialization;

namespace PiratesOfBermuda
{
    //public enum GameState { Playing, Paused, PlayerLose, PlayerWin, AllDead}

    public class SinglePlayerScreen : GameScreen
    {
        #region Initialize
        // Lưu ý biến gameProcessor duoc khai bao trong file GameScreen

        public SinglePlayerScreen()
        {
            // Chi nhan tu touch sreen dong tac nhan
            EnabledGestures = GestureType.Tap;        
        }
     
        /// <summary>
        /// Ham dung de load resources can thiet cho gameplay
        /// Khoi tao doi tuong Player, va cac doi tuong khac tren ban do
        /// </summary>
        public override void LoadContent()
        {
            gameProcessor = new SinglePlayerGameProcessor(ScreenManager.Game, ScreenManager.SpriteBatch);
            gameProcessor.backgroundTexture = Load<Texture2D>(@"Textures/Backgrounds/GameplayBackground");
            gameProcessor.explodeTexture = Load<Texture2D>(@"Textures/MainGameplay/cannonblast");
            //Kiem tra xem co game nao duoc saved nao khong
            //Neu co thi load no len
            if (PhoneApplicationService.Current.State.ContainsKey("IsReload"))
            {
                string[] keys = PhoneApplicationService.Current.State.Keys.ToArray();

                for(int i = 0; i < keys.Length ; i++)
                {
                    string key = keys[i];
                    string[] split = key.Split('_');
                    if (split.Length == 1)
                        continue;
                    int posX = int.Parse(split[0]);
                    int posY = int.Parse(split[1]);
                    ((SinglePlayerGameProcessor) gameProcessor).LoadGameObject(key, posX, posY);
                    
                    PhoneApplicationService.Current.State.Remove(key);
                }
                gameProcessor.player.LifeRemained = (int)PhoneApplicationService.Current.State["PlayerLifeRemained"];
                PhoneApplicationService.Current.State.Remove("PlayerLifeRemained");
                PhoneApplicationService.Current.State.Remove("IsReload");
            }
                //Nguoc lai thi khoi tao mot game moi
            else
            {
                ((SinglePlayerGameProcessor)gameProcessor).InitGame();
            }
            base.LoadContent();
        }
 
        #endregion   
        
        #region Update
        /// <summary>
        /// Ham cap nhat sau moi nuoc di cua nguoi choi
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="otherScreenHasFocus"></param>
        /// <param name="coveredByOtherScreen"></param>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {            
            gameProcessor.Update(gameTime);

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }                
        #endregion

        #region Draw                        
        /// <summary>
        /// Ham thu ly su kien Draw
        /// </summary>
        /// <param name="gameTime"></param>
        public override void Draw(GameTime gameTime)
        {
            gameProcessor.Draw(gameTime, ScreenManager.font);
        }
        #endregion

        #region Player Helper Method     
        protected void PauseCurrentGame()
        {
            var pauseMenuBackground = new BackgroundScreen();

            ScreenManager.AddScreen(pauseMenuBackground);

            /// @Duy Quang: Luu y doi voi SinglePlayerScreen thi:
            /// ScreenManager.AddScreen(new PauseScreen(this, pauseMenuBackground, true));
            ScreenManager.AddScreen(new PauseScreen(this, pauseMenuBackground, true));
        }
           
        /// <summary>
        /// Xu ly thong tin nhan duoc tu nguoi choi
        /// </summary>
        /// <param name="input"></param>
        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");
            if (input.IsPauseGame())
            {
                PauseCurrentGame();
            }
            gameProcessor.HandleInput(input.Gestures);
            if (gameProcessor.gameState != GameState.Playing)
            {
                foreach (GestureSample gestureSample in input.Gestures)
                {
                    if (gestureSample.GestureType == GestureType.Tap)
                    {
                        ScreenManager.AddScreen(new GameOverScreen(this, false));
                    }
                }
            }
        }
        #endregion
    }
}
